import { _BasicDataSource } from "../../_BasicDataSource"
import { uuid } from '../../util'

import { BaseEntity } from "../Entitys/BaseEntity";
import { Label } from "../Entitys/_Label";
import { Link } from '../Entitys/_Link'
import { COLOR, GetCenter, formarColor } from "../../Config";


class LinkEntity {
    constructor(PlatformA, PlatformB, parent, options) {
        this.id = 'LinkEntity_' + PlatformA.id + '_' + PlatformB.id;
        this.parent = parent;

        this.PlatformA = PlatformA;
        this.PlatformB = PlatformB;

        var ArcType = parent.options.ArcType || Cesium.ArcType.NONE
        var width = parent.options.width == null || parent.options.width == undefined ? 0 : 1;
        var color = parent.options.color;
        var text = parent.options.text || '';
        var textColor = parent.options.textColor || '#ffffff';

        this.options = {
            ArcType: options.ArcType || ArcType,
            width: options.width || width,
            color: options.color || color,
            name: {
                text: options.text || text,
                color: options.textColor || textColor,
                background: options.color || color,
            },
        }

        this.EntityTypeName = 'LinkEntity';
        // 基础平台类型
        this.BaseEntity = new BaseEntity({});
        this.BaseEntity.CesiumPosition = GetCenter([this.PlatformA.BaseEntity.CesiumPosition, this.PlatformB.BaseEntity.CesiumPosition]);



        // 显示名称类型
        this.Label = new Label(this.BaseEntity.Entity, this.options.name);
        this.Label.show = parent.options.LabelStatus;

        this.Link = new Link(this.BaseEntity.Entity, this.PlatformA.BaseEntity.CesiumPosition, this.PlatformB.BaseEntity.CesiumPosition, this.options)

        // 输出用于图层加载的对象
        this.Entity = this.BaseEntity.Entity;
        this.Entity.position.setValue(this.BaseEntity.CesiumPosition);
        this.PlatformA.addLink(this);
        this.PlatformB.addLink(this);
    }
    setPosition() {
        this.BaseEntity.CesiumPosition = GetCenter([this.PlatformA.BaseEntity.CesiumPosition, this.PlatformB.BaseEntity.CesiumPosition]);
        this.Entity.position.setValue(this.BaseEntity.CesiumPosition);
        this.Link.setPosition(this.PlatformA.BaseEntity.CesiumPosition, this.PlatformB.BaseEntity.CesiumPosition);
    }
    destroy() {
        this.PlatformA.removeLink(this);
        this.PlatformB.removeLink(this);
    }
    set Width(value) {
        this.options.width = value;
        this.Entity.polyline.width = value;
    }
    get Width() {
        return this.Entity.polyline.width
    }
    set Color(value) {
        var _color = formarColor(value);
        this.Entity.polyline.material = _color;
        this.Entity.label && (this.Entity.label.backgroundColor = _color)
    }
    get Color() {
        return this.parent.options.color
    }
}



class LinkDataSource extends _BasicDataSource {
    constructor(parent, options, parentDataSource) {
        super(uuid(), parent, options);
        this.parentDataSource = parentDataSource;
        this.options = options || {};
        this.options.LabelStatus = options.LabelStatus === undefined ? true : options.LabelStatus;
    }

    /**
     * 此处覆盖了父级的 Adds 方法，自行实现对应内容
     *
     * @param {*} platformArray
     * @memberof LinkDataSource
     */
    Adds(linkArray, options = {}) {
        this._Entities.suspendEvents();
        // 获取现有的线的集合
        var linkKeyArray = Object.keys(this._Mapping);
        // 获取已经数据中已经存在的显得集合
        var hasKeyArray = [];
        // 遍历数据源
        linkArray.forEach(item => {
            var ids = item[0] + '_' + item[1];
            var rids = item[1] + '_' + item[0];
            // 因为链接线不分正反向，所以需要判断反向数据源关系
            if (!this._Mapping[ids] && !this._Mapping[rids]) {
                // 从父级图层中获取对应的平台元素数据源
                var A = this.parentDataSource.GetMappingById(item[0]);
                var B = this.parentDataSource.GetMappingById(item[1]);
                // 如果两个数据源都存在，则创建，否则跳过，不进行操作
                if (A && B) {
                    const _LinkEntity = new LinkEntity(A, B, this, options);
                    hasKeyArray.push(ids);
                    this._Mapping[ids] = _LinkEntity;
                    this._Entities.add(_LinkEntity.Entity);
                }
            } else {
                // 不管正向或者反向，均进行记录
                hasKeyArray.push(ids);
            }
        });
        // 进行数据源去重，删除已经存在的，和新创建的连接线对象ID
        hasKeyArray.forEach((item) => {
            if (linkKeyArray.includes(item)) {
                var index = linkKeyArray.indexOf(item);
                linkKeyArray.splice(index, 1);
            }
        });
        // 删除元素
        linkKeyArray.forEach(item => {
            this.Remove(item);
        });
        this._Entities.resumeEvents();
    }
    set Color(value) {
        this.EachMapping((item) => {
            item.Color = value;
        })
    }
    get Color() {
        return this.options.color
    }
    set Width(value) {
        this.EachMapping((item) => {
            item.Width = value;
        })
    }
    get Width() {
        return this.options.width
    }
    set LabelStatus(value) {
        this.options.defaultNameStatus = value;
        this.EachMapping(item => {
            item.Label.show = !!value;
        });
    }
    get LabelStatus() {
        return this.options.defaultNameStatus;
    }
    showLabel(type) {
        this.EachMapping(item => {
            item.Label.show = !!type;
        });
    }

}

export {
    LinkEntity,
    LinkDataSource
}
